package com.beehoo.dcportal.admin.action;

import java.util.List;

import com.beehoo.dcportal.admin.service.WorkerService;
import com.beehoo.dcportal.common.Constant;
import com.beehoo.dcportal.common.base.BaseAction;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.model.FunctionBean;
import com.beehoo.dcportal.model.WorkerBean;
import com.beehoo.dcportal.utils.CommonUtils;
import com.beehoo.dcportal.utils.FunctionTree;

public class WorkerAction extends BaseAction {

	private static final long serialVersionUID = 7999681714250102083L;
	
	private WorkerService workerService;
	private String loginName;
	private String password;
	private String validateCode;
	private WorkerBean workerBean;
	private List<FunctionBean> functionList;
	private boolean sso;

	public boolean isSso() {
		return sso;
	}

	public void setSso(boolean sso) {
		this.sso = sso;
	}

	public List<FunctionBean> getFunctionList() {
		return functionList;
	}

	public void setFunctionList(List<FunctionBean> functionList) {
		this.functionList = functionList;
	}

	public String getValidateCode() {
		return validateCode;
	}

	public void setValidateCode(String validateCode) {
		this.validateCode = validateCode;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public WorkerBean getWorkerBean() {
		return workerBean;
	}

	public void setWorkerBean(WorkerBean workerBean) {
		this.workerBean = workerBean;
	}

	public void setWorkerService(WorkerService workerService) {
		this.workerService = workerService;
	}

	public String login(){
		String result = SUCCESS;
		String sValidateCode = CommonUtils.nvlToString((String) getSession().get(Constant.SESSION_VALIDATECODE));
		getSession().remove(Constant.SESSION_VALIDATECODE);
		
		if(!sso){
			if (CommonUtils.nvlToString(validateCode).equals("")) {
				setMessage(super.getText("custom.messages.error.validatecode.empty"));
				return ERROR;
			}
			if (!CommonUtils.nvlToString(validateCode).equalsIgnoreCase(sValidateCode)) {
				setMessage(super.getText("custom.messages.error.validatecode.error"));
				return ERROR;
			}
		}
		if(CommonUtils.nvlToString(loginName).equals("")){
			setMessage(super.getText("custom.messages.error.user.name.empty"));
			return ERROR;
		}
		if(CommonUtils.nvlToString(password).equals("")){
			setMessage(super.getText("custom.messages.error.user.pwd.empty"));
			return ERROR;
		}
		try {
			workerBean = workerService.queryUserByNP(loginName, password);
			if(workerBean == null){
				setMessage(super.getText("custom.messages.error.user.name.pwd.error"));
				result = ERROR;
			}else{
				String userId = CommonUtils.nvlToString(workerBean.getUserId());
				String userRoleId = CommonUtils.nvlToString(workerBean.getRoleId());
				String streetId = CommonUtils.nvlToString(workerBean.getStreetId());
				if(userId.equals("") || streetId.equals("")){
					setMessage(super.getText("custom.messages.error.user.name.pwd.error"));
					result = ERROR;
				}else if(userRoleId.equals("")){
					setMessage(super.getText("custom.messages.error.user.role.empty"));
					result = ERROR;
				}else{
					getSession().put(Constant.SESSION_STREET_ID, streetId);
					getSession().put(Constant.SESSION_USER_ID, userId);
					getSession().put(Constant.SESSION_USER_NAME, loginName);
					getSession().put(Constant.SESSION_USER_ROLE, userRoleId);
					getSession().put(Constant.SESSION_USER_IPIMSI, CommonUtils.getIpAddr(getRequest()));
				}
			}
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
			result = ERROR;
		}
		return result;
	}
	
	public String logout(){
		getSession().clear();
		super.getRequest().getSession().invalidate();
		return "login";
	}
	
	public String tree(){
		String result = "tree";
		String userRoleId = (String) getSession().get(Constant.SESSION_USER_ROLE);
		try {
			functionList = workerService.queryFunction(userRoleId);
			String funcionTree = FunctionTree.buildFunctionTree(functionList);
			getRequest().setAttribute("funcionTree", funcionTree);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
			result = "login";
		}
		return result;
	}
}
